package tt.dz.egobus.servicemanager.bean;

import org.hibernate.Criteria;
import tt.dz.egobus.bean.CriteriaRestrictionsBuilder;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Anson
 * @date 17-3-14
 * @name CriteriaRestrictionsBuilder 帮助类
 * @since 1.0.1
 */
public final class CriteriaRestrictionHelper {

    /**
     * @param criteria
     * @param params   带有一定查询匹配模式的key的查询参数
     * @author Anson
     * @date 2017年03月14日13:58:42
     * @name Criteria 帮助类
     * @since 1.0.1
     */
    public static final CriteriaRestrictionsBuilder packageParamCriteria(Criteria criteria,
                                                                         Map<String, Object> params) {
        CriteriaRestrictionsBuilder criteriaRestBuilder = new CriteriaRestrictionsBuilder(criteria);
        if (null != params && !params.isEmpty()) {
            Map.Entry<String, Object> entry = null;
            for (Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
                 iterator.hasNext(); ) {
                entry = iterator.next();
                criteriaRestBuilder.contact(entry.getKey(), entry.getValue());
            }
        }
        return criteriaRestBuilder;
    }

    /**
     * @param criteriaRestBuilder 查询构造器
     * @param orderList 排序规则表达式
     * @author Anson
     * @date 17-3-14
     * @name
     * @since 1.0.1
     */
    public static final void packageOrders(CriteriaRestrictionsBuilder criteriaRestBuilder, List<String> orderList) {
        //添加排序
        if (null != orderList && !orderList.isEmpty()) {
            for (String order : orderList) {
                criteriaRestBuilder.order(order);
            }
        }
    }

}
